//
//  MainViewController.m
//  CBT
//
//  Created by 四点钟 on 2018/11/16.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "MainViewController.h"
#import "RechargeViewController.h"
#import "EOSRechargeViewController.h"
#import "TransferViewController.h"

#import "MainViewModel.h"
#import "WCMainSubCell.h"
#import "NoDateCell.h"
#import "JHUIAlertView.h"
#import "UserManager.h"

@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic, assign) int               page;
@property (nonatomic, strong) NSMutableArray    *dataArray;
@property (nonatomic, assign) BOOL              isScrollEnabled;

@property (nonatomic, strong) UITableView   *tableView;
@property (nonatomic, strong) UIImageView   *bgImageView;

@property (nonatomic, strong) MainModel     *mainModel;
@property (nonatomic, strong) MainViewModel *viewModel;
@property (nonatomic, strong) MainNewsModel *mainNewsModel;

@property (nonatomic, strong) NoDataView    *noNetWorkView;
@property (nonatomic, strong) MainTopView   *mainTopView;
@property (nonatomic, strong) JHUIAlertView *alertView;

@property (nonatomic, weak) CoinInfoItemModel *coinInfoModel;

@property (nonatomic, strong) UIView *chooseBgView; //兑换选择币view
@property (nonatomic, strong) UITextField *coinTF;
@property (nonatomic, copy) NSString *chooseCoinIdStr;
@property (nonatomic, strong) NSMutableArray *exchangeArr;

@end

@implementation MainViewController

#pragma mark - VC Life

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.exchangeArr = [[NSMutableArray alloc]init];
    self.page = 1;
    self.isScrollEnabled = NO;
    [self startClearBgColorLoading];
    
    self.fd_prefersNavigationBarHidden = YES;
    
    [self.view addSubview:self.tableView];
    
    [self bindVideoDetailsData];
    [self.viewModel exchangeData];
    [self.viewModel getMainNewsList];
   
    [self.view insertSubview:self.bgImageView belowSubview:self.tableView];
}

- (void)bindVideoDetailsData {
    @weakify(self);
    //首页已上架币种请求成功
    [self.viewModel.listSuccessObject subscribeNext:^(MainModel *x) {
        [self_weak_ stopLoading];
        self_weak_.mainModel = x;
        if (self_weak_.page == 1) {
            self_weak_.dataArray = [NSMutableArray array];
        }
        [self_weak_.dataArray addObjectsFromArray:x.coinInfo];
        self_weak_.isScrollEnabled = [NSArray isArrayEmpty:self_weak_.dataArray];
        if (self_weak_.dataArray.count > 0) {
            self_weak_.tableView.mj_footer.hidden = NO;
            [self_weak_.noNetWorkView removeFromSuperview];
            self_weak_.page = self_weak_.page + 1;
            if (self_weak_.page > [self_weak_.mainModel.totalPages intValue]) {
                [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [self_weak_.tableView.mj_footer endRefreshing];
            }
        }
        else {
            self_weak_.noNetWorkView.imageView.image = [UIImage imageNamed:kLocalizedString(@"zanwushuju_c", nil)];
            [self_weak_.tableView addSubview:self_weak_.noNetWorkView];
        }
        [self_weak_.tableView.mj_header endRefreshing];
        [self_weak_.tableView reloadData];
    }];
    
    //首页已上架币种请求失败
    [self.viewModel.listFailureObject subscribeNext:^(id  _Nullable x) {
        [self_weak_ stopLoading];
        [self_weak_.tableView.mj_header endRefreshing];
        [self_weak_.tableView.mj_footer endRefreshing];
        if (self_weak_.page == 1 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
            self_weak_.tableView.mj_footer.hidden = YES;
            self_weak_.isScrollEnabled = YES;
            self_weak_.noNetWorkView.imageView.image = [UIImage imageNamed:kLocalizedString(@"duanwang_c", nil)];
            [self_weak_.tableView addSubview:self_weak_.noNetWorkView];
        }else{
            self_weak_.isScrollEnabled = NO;
            [self_weak_.noNetWorkView removeFromSuperview];
        }
        [self_weak_.tableView reloadData];
    }];
    
    //首页收益请求
    [self.viewModel.incomeSuccessObject subscribeNext:^(MainIncomeModel *x) {
        self_weak_.mainTopView.yesterdayEaLabel.text = [DecimalNumberTool formatNumberWithString:x.yesterdayIncome isAssets:NO];
        self_weak_.mainTopView.totalLIncomeCount.text = [DecimalNumberTool formatNumberWithString:x.totalIncome isAssets:NO];;
    }];
    
    //首页公告请求
    [self.viewModel.bulletinSuccessObject subscribeNext:^(MainNewsModel *x) {
        self_weak_.mainNewsModel = x;
        [self_weak_.mainTopView.rollView setNewArray:x.textArray];
    }];
}

#pragma mark - ChangeLanguageNotification
- (void)changeLanguage {
    [self.tableView reloadData];
    
    if (self.noNetWorkView.isDisplayedInScreen) {
        self.noNetWorkView.imageView.image =  [UIImage imageNamed:[[AFNetworkReachabilityManager sharedManager] isReachable] ? kLocalizedString(@"zanwushuju_c", nil) : kLocalizedString(@"duanwang_c", nil)];
    }
    
    [self.viewModel getMainNewsList];
}

//兑换
- (void)doExchangeWithModel:(CoinInfoItemModel *)model {
    
    CoinInfoItemModel *coinModel = [[CoinInfoItemModel alloc]init];
    if (self.exchangeArr.count > 0) {
        coinModel = self.exchangeArr.firstObject;
    }
    self.chooseCoinIdStr = coinModel.coinId;
    
    self.coinInfoModel = model;
    JHUIAlertConfig *config = [[JHUIAlertConfig alloc] init];
    config.title.text  = kLocalizedString(@"兑换", nil);
    config.title.color = UIColorFromRGB(0xff5101);
    config.title.bottomPadding = 100;
    config.dismissWhenTapOut   = NO;
    config.contentViewWidth    = SCREEN_WIDTH*0.8;
    config.contentViewHeight   = 180;
    self.alertView = [[JHUIAlertView alloc] initWithConfig:config];
    
    __weak typeof(self)weakself = self;
    [self.alertView addCustomView:^(JHUIAlertView *alertView, CGRect contentViewRect, CGRect titleLabelRect, CGRect contentLabelRect) {
        
        UIButton *closebtn = [UIButton buttonWithType:UIButtonTypeCustom];
        closebtn.tintColor = [UIColor blackColor];
        [closebtn setImage:[UIImage imageNamed:@"loginClose"] forState:UIControlStateNormal];
        closebtn.frame = CGRectMake(contentViewRect.size.width-30, 10, 20, 20);
        [closebtn addTarget:weakself action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
        [alertView.contentView addSubview:closebtn];

        UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabelRect)+24, contentViewRect.size.width-30, 40)];
        grayView.backgroundColor = UIColorFromRGB(0xEAEAEA);
        grayView.layer.cornerRadius = 5;
        [alertView.contentView addSubview:grayView];

        weakself.coinTF = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLabelRect)+24, contentViewRect.size.width-80, 40)];
        weakself.coinTF.backgroundColor = UIColorFromRGB(0xEAEAEA);
        weakself.coinTF.keyboardType = UIKeyboardTypeDecimalPad;
        weakself.coinTF.layer.cornerRadius = 5;
        [alertView.contentView addSubview:weakself.coinTF];
        weakself.coinTF.placeholder = [NSString stringWithFormat:@"%@%@ %@%@%.4f",kLocalizedString(@"兑换成", nil),coinModel.coinSymbol,model.coinSymbol,kLocalizedString(@"可用数量:", nil),[model.coinAmount floatValue]];
        weakself.coinTF.font = [UIFont systemFontOfSize:15];
        
        UIButton *chooseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        chooseBtn.tintColor = [UIColor blackColor];
        [chooseBtn setImage:[UIImage imageNamed:@"sanjiao_xia"] forState:UIControlStateNormal];
        [chooseBtn addTarget:weakself action:@selector(chooseBtnXiaAct) forControlEvents:UIControlEventTouchUpInside];
        chooseBtn.frame = CGRectMake(contentViewRect.size.width-40-20, CGRectGetMaxY(titleLabelRect)+24, 40, 40);
        [alertView.contentView addSubview:chooseBtn];

        UILabel *ratioLabel = [[UILabel alloc] init];
        [alertView.contentView addSubview:ratioLabel];
        
        ratioLabel.text = kLocalizedString(@"手续费 5%", nil);
        ratioLabel.font = [UIFont systemFontOfSize:12];
        ratioLabel.textColor     = [UIColor lightGrayColor];
        ratioLabel.textAlignment = NSTextAlignmentRight;
        ratioLabel.frame = CGRectMake(CGRectGetMaxX(titleLabelRect) - 75, CGRectGetMaxY(titleLabelRect)+70, 60, 12);
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(15, CGRectGetMaxY(weakself.coinTF.frame)+34, contentViewRect.size.width-30, 40);
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        button.layer.cornerRadius = 5;

        [button setTitle:kLocalizedString(@"确定", nil) forState:0];
        [button setTitleColor:[UIColor whiteColor] forState:0];
        [button addTarget:weakself action:@selector(buttonEvent) forControlEvents:1<<6];
        
        [alertView.contentView addSubview:button];
        
    }];
    
    [self.view addSubview:self.alertView];
}

//选择兑换币种
- (void)chooseBtnXiaAct{
    __weak typeof(self)weakself = self;
    [self.alertView addCustomView:^(JHUIAlertView *alertView, CGRect contentViewRect, CGRect titleLabelRect, CGRect contentLabelRect) {
        
        weakself.chooseBgView = [[UIView alloc]init];
        weakself.chooseBgView.frame = CGRectMake((SCREEN_WIDTH-SCREEN_WIDTH*0.8)/2.0, contentViewRect.origin.y, SCREEN_WIDTH*0.8, 250);
        [alertView addSubview:weakself.chooseBgView];
        weakself.chooseBgView.backgroundColor = [UIColor whiteColor];
        weakself.chooseBgView.layer.masksToBounds = YES;
        weakself.chooseBgView.layer.cornerRadius = 10.0;
        
        UILabel *titleLB = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, contentViewRect.size.width-100, 22)];
        titleLB.text = kLocalizedString(@"兑换", nil);
        titleLB.textColor = UIColorFromRGB(0xFF754A);
        titleLB.font = [UIFont systemFontOfSize:18 weight:1];
        titleLB.textAlignment = NSTextAlignmentCenter;
        [weakself.chooseBgView addSubview:titleLB];
        
        UIButton *closebtn = [UIButton buttonWithType:UIButtonTypeCustom];
        closebtn.tintColor = [UIColor blackColor];
        [closebtn setImage:[UIImage imageNamed:@"loginClose"] forState:UIControlStateNormal];
        [closebtn addTarget:weakself action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
        closebtn.frame = CGRectMake(contentViewRect.size.width-30, 10, 20, 20);
        [weakself.chooseBgView addSubview:closebtn];
        
        UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabelRect)+24, contentViewRect.size.width-30, 120)];
        grayView.backgroundColor = UIColorFromRGB(0xEAEAEA);
        grayView.layer.cornerRadius = 5;
        [weakself.chooseBgView addSubview:grayView];
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLabelRect)+24, contentViewRect.size.width-80, 40)];
        textField.backgroundColor = UIColorFromRGB(0xEAEAEA);
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.layer.cornerRadius = 5;
        [weakself.chooseBgView addSubview:textField];
        textField.placeholder = weakself.coinTF.placeholder;
        textField.userInteractionEnabled = NO;
        textField.font = [UIFont systemFontOfSize:15];
        
        UIButton *chooseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        chooseBtn.tintColor = [UIColor blackColor];
        [chooseBtn setImage:[UIImage imageNamed:@"sanjiao_shang"] forState:UIControlStateNormal];
        [chooseBtn addTarget:weakself action:@selector(chooseBtnShangAct) forControlEvents:UIControlEventTouchUpInside];
        chooseBtn.frame = CGRectMake(contentViewRect.size.width-40-20, CGRectGetMaxY(titleLabelRect)+24, 40, 40);
        [weakself.chooseBgView addSubview:chooseBtn];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLabelRect)+24+40, contentViewRect.size.width-60, 0.5)];
        [weakself.chooseBgView addSubview:lineView];
        lineView.backgroundColor = [UIColor colorWithWhite:0.66 alpha:1];
        
        UIScrollView *coinScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLabelRect)+24+50, contentViewRect.size.width-60, 60)];
        [weakself.chooseBgView addSubview:coinScrollView];
        coinScrollView.contentSize = CGSizeMake(contentViewRect.size.width-60, 30*weakself.exchangeArr.count);
        
        for (int i = 0; i < self.exchangeArr.count; i++) {
            CoinInfoItemModel *coinModel = weakself.exchangeArr[i];
            UIButton *akbBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            akbBtn.frame = CGRectMake(0, 30*i, 200, 30);
            [coinScrollView addSubview:akbBtn];
            [akbBtn setTitleColor:[UIColor colorWithWhite:0.33 alpha:1] forState:UIControlStateNormal];
            akbBtn.titleLabel.font = [UIFont systemFontOfSize:15];
            [akbBtn setTitle:coinModel.coinSymbol forState:UIControlStateNormal];
            [akbBtn addTarget:weakself action:@selector(akbBtnAct:) forControlEvents:UIControlEventTouchUpInside];
            akbBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            akbBtn.tag = 1000+i;
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(15, 200, contentViewRect.size.width-30, 40);
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitle:kLocalizedString(@"确定", nil) forState:0];
        [button setTitleColor:[UIColor whiteColor] forState:0];
        button.layer.cornerRadius = 5;
        button.backgroundColor = UIColorFromRGB(0xff5001);
        [button addTarget:weakself action:@selector(buttonEvent) forControlEvents:1<<6];
        [weakself.chooseBgView addSubview:button];
    }];
    
}

- (void)chooseBtnShangAct{
    [self.chooseBgView removeFromSuperview];
}

- (void)akbBtnAct:(UIButton *)sender{
    CoinInfoItemModel *coinModel = self.exchangeArr[sender.tag - 1000];

    self.coinTF.placeholder = [NSString stringWithFormat:@"%@%@：%.4f",coinModel.coinSymbol,kLocalizedString(@"可用", nil),[coinModel.coinAmount floatValue]];
    
    self.coinTF.placeholder = [NSString stringWithFormat:@"%@%@ %@%@%.4f",kLocalizedString(@"兑换成", nil),coinModel.coinSymbol,self.coinInfoModel.coinSymbol,kLocalizedString(@"可用数量:", nil),[self.coinInfoModel.coinAmount floatValue]];
    
    self.chooseCoinIdStr = [NSString stringWithFormat:@"%@",coinModel.coinId];
    [self chooseBtnShangAct];
}

- (void)closeView {
    [self.alertView dismiss];
    [self.view endEditing:YES];
}

- (void)buttonEvent {
    NSString *text = self.coinTF.text;
    if (text.length <= 0) {
        [HFStatusView showFailureMessage:kLocalizedString(@"请输入兑换数量", nil)];
        return;
    }
    if ([self.coinInfoModel.coinAmount floatValue] < [text floatValue]) {
        [HFStatusView showFailureMessage:kLocalizedString(@"数量不足", nil)];
        return;
    }
    if ([self.coinTF.text floatValue] <= 0) {
        return;
    }
    [self.view endEditing:YES];
    [self.alertView dismiss];
    
    NSDictionary *dic = @{@"userId":[CBTLocalUserManager sharedInstance].userId,
                          @"coinId":self.coinInfoModel.coinId,
                          @"value":self.coinTF.text,
                          @"toCoinId":self.chooseCoinIdStr
                          };
    __weak typeof(self)weakself = self;
    [UserManager doPCExchangeInfo:dic block:^(id data, HttpRequestResult result, NSString *msg) {
        weakself.coinTF.text = @"0";
        [HFStatusView showFailureMessage:data[@"message"]];
        if (result == kHttpRequestResultNormal) {
            [weakself.tableView.mj_header beginRefreshing];
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CoinInfoItemModel *coinModel = self.dataArray[indexPath.row];
    
    if ([coinModel.coinSymbol.lowercaseString isEqualToString:@"pc"]) {
        WCMainSubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainsubCell"];
        cell.model = self.dataArray[indexPath.row];
        __weak typeof(self) weakSelf = self;
        [cell setMainCellBlock:^(NSInteger type,CoinInfoItemModel *model) {
            if (type == 0) {
                UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                RechargeViewController *vc = [story instantiateViewControllerWithIdentifier:@"rechargeVC"];
                vc.coinId   = model.coinId;
                vc.coinName = model.coinSymbol;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if(type == 1) {
                self.exchangeArr = [[NSMutableArray alloc]init];
                for (int i = 0; i < self.dataArray.count; i++) {
                    if (i != indexPath.row) {
                        [self.exchangeArr addObject:self.dataArray[i]];
                    }
                }
                
                [self doExchangeWithModel:model];
            }
            else {
                UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                TransferViewController *vc = [story instantiateViewControllerWithIdentifier:@"TransferID"];
                vc.balance  = [model.coinAmount stringValue];
                vc.coinId   = model.coinId;
                vc.navTitle = model.coinSymbol;
                [vc setTransferSuccessBlock:^{
                    weakSelf.page = 1;
                    [weakSelf.viewModel exchangeData];
                }];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }];
        return cell;
    }
    else {
        MainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell"];
        cell.model = self.dataArray[indexPath.row];
        __weak typeof(self) weakSelf = self;
        [cell setMainCellBlock:^(NSInteger type,CoinInfoItemModel *model) {
            if (type == 0) {
                UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                RechargeViewController *vc = [story instantiateViewControllerWithIdentifier:@"rechargeVC"];
                vc.coinId   = model.coinId;
                vc.coinName = model.coinSymbol;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else {
                UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                TransferViewController *vc = [story instantiateViewControllerWithIdentifier:@"TransferID"];
                vc.balance  = [model.coinAmount stringValue];
                vc.coinId   = model.coinId;
                vc.navTitle = model.coinSymbol;
                [vc setTransferSuccessBlock:^{
                    weakSelf.page = 1;
                    [weakSelf.viewModel exchangeData];
                }];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }];
        return cell;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.mainTopView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 160;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

#pragma mark - UIScrollerViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat off_y = scrollView.contentOffset.y;
    if (self.isScrollEnabled && off_y > 0) {
        [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    
    self.mainTopView.rootImageView.hidden = scrollView.contentOffset.y <= 0;
}

#pragma mark - Lazy
-(MainViewModel *)viewModel
{
    if (!_viewModel) {
         _viewModel = [[MainViewModel alloc] init];
    }
    return _viewModel;
}

- (NoDataView *)noNetWorkView
{
    if (!_noNetWorkView) {
        @weak(self)
        _noNetWorkView = [[NoDataView alloc]initWithFrame:CGRectMake(0, 165, ScreenNarrowSide, CGRectGetHeight(self_weak_.tableView.bounds) - 165) type:(ShowType_NoData) clickBlock:^{}];
    }
    return _noNetWorkView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenNarrowSide, 190)];
        _bgImageView.image = [UIImage imageNamed:@"wode_beijing"];
    }
    return _bgImageView;
}

- (MainTopView *)mainTopView
{
    if (!_mainTopView) {
        __weak MainViewController *weakSelf = self;
        _mainTopView = [[MainTopView alloc]initWithFrame:CGRectMake(0, 0, ScreenWideSide, 128)];
        [_mainTopView setMainTopViewBlock:^(NSInteger type,NSInteger indexPath) {
            if (type == 0) {
                UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                PlacardViewController *vc = ((PlacardViewController *)[story instantiateViewControllerWithIdentifier:@"placardVC"]);
                vc.isOneCell = NO;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
            else if(type == 1) {
                if (![NSObject dx_isNullOrNilWithObject:weakSelf.mainNewsModel]) {
                    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                    PlacardViewController *vc = ((PlacardViewController *)[story instantiateViewControllerWithIdentifier:@"placardVC"]);
                    vc.isOneCell = YES;
                    vc.oneCellModel = weakSelf.mainNewsModel.newsInfo[indexPath];
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                }
            }
        }];
    }
    return _mainTopView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height - 40) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }else{
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.estimatedRowHeight           = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
//        [_tableView registerClass:[MainSectionCell class] forCellReuseIdentifier:@"SectionCell"];
        [_tableView registerClass:[MainCell class] forCellReuseIdentifier:@"mainCell"];
        [_tableView registerClass:[WCMainSubCell class] forCellReuseIdentifier:@"mainsubCell"];
        [_tableView registerClass:[NoDateCell class] forCellReuseIdentifier:@"NoDataCellID"];
        
        __weak MainViewController *weakSelf = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.page = 1;
            [weakSelf.viewModel exchangeData];
            [weakSelf.viewModel getMainNewsList];
        }];
    
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            [weakSelf footerRefresh];
//        }];
        
        _tableView.mj_footer.hidden = YES;
        
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).triggerAutomaticallyRefreshPercent = 50;
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).automaticallyRefresh = NO;
        ((MJRefreshAutoNormalFooter *)_tableView.mj_footer).onlyRefreshPerDrag = YES;
    }
    return _tableView;
}



@end
